(in-package "ACL2")
(include-book "world")
(include-book "grid")
(include-book "image-handler")
(include-book "../../include/src/io-utilities")
(set-state-ok t)
;;;The commadn handler responds to button presses
;;;read in by the command parser. It calls the appropriate
;;;function to update hte world.

(defconst *ff-speed* 60)

(defconst *play-speed* 10)

(defconst *pause-speed* 0)

;;Update the world's framerate to "play" speed
(defun play (world)
    (set-world-framerate world *play-speed*))

;;Update the world's framerate to "pause" speed
(defun pause (world)
    (set-world-framerate world *pause-speed*))

;;Update the world's framerate to "fast-forward" speed
(defun fast-forward (world)
    (set-world-framerate world *ff-speed*))

;;Check the type of the world's rules (helper)
(defun check-rules-type* (world)
   (equal (ruleset-rule-type (cell-world-rules world)) "lifelike"))

;;Tell the image handler to save an image.
(defun handle-save-img (world fname-chrs state)
   (mv-let (err state)
           (image-save (cell-world-grid world)
                       (check-rules-type* world)
                       (chrs->str fname-chrs)
                       state)
           (if err (mv world state) (mv world state))))

;;Tell the image handler to load an image.
(defun handle-load-img (world fname-chrs state)
   (mv-let (new-grid state)
           (image-load (chrs->str fname-chrs) state)
		 (mv (set-world-grid world new-grid) state)))
                       
;;Set a cell based on the world type
(defun set-cell (world x y val)
    (let* ((rules (cell-world-rules world))
    		(ruletype (ruleset-rule-type rules))
    		(grid (cell-world-grid world)))
        (cond ((equal ruletype "lifelike")
                (set-world-grid world
                    (lifelike-grid-set grid x y val)))
              ((equal ruletype "elementary")
                (set-world-grid world
                    (elementary-grid-set grid x val)))
              (t "error in set-cell"))))